package org.authsum.stitches.wicket;

import java.util.Iterator;
import java.util.List;

import javax.persistence.Transient;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.GenericValidator;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;

public abstract class DetailPanel extends BasePanel  {
	//public abstract Object getDetailObject();
	
	protected void postInit() {
		getFeedbackPanel().setOutputMarkupId(true);
	}
	protected void doValidatedSave(AjaxRequestTarget target) {
		logger.debug("doValidatedSave");
		

		//publishable.setModifiedBySite(site);
		if (!validateDetailObject(target)) {
			logger.debug("failed validate");
			return;
		}
		
		
		
		
		saveDetailObject(target);
		if (target != null) {
			target.addComponent(getFeedbackPanel());
		}
	}
	
	@Transient
	protected void check(String fieldname,String fieldvalue,int length,List<String> messages,boolean required) {
		if (required) {
			if (GenericValidator.isBlankOrNull(fieldvalue)) {
				messages.add(fieldname+  " is required.");
			}
		}
		
		
		if (fieldvalue != null && fieldvalue.length() > length) {
			messages.add(fieldname + " must not exceed " + length + " characters.  Current length is " + fieldvalue.length() + " characters.");


		}
		


	}
	
	
	private static Log logger = LogFactory.getLog(DetailPanel.class);
	protected Form form = null;

	public boolean validateDetailObject(AjaxRequestTarget target) {
		//IPublishable publishable = (IPublishable)getDetailObject();
		
		List<String> errors = getValidity();
		if (!errors.isEmpty()) {

			// this.feedbackPanel.
			// this.error(message)

			Iterator<String> iterator = errors.iterator();
			while (iterator.hasNext()) {
				error(iterator.next());
			}
			if (target != null) {
				target.addComponent(getFeedbackPanel());
			}

			return false;
		}
		return true;

	}

	
	protected abstract List<String> getValidity();
	
	
	protected abstract void saveDetailObject(AjaxRequestTarget target) ;
	//all the detail panels show the name of the object in the header
	protected Label nameLabel = null;
	protected PropertyModel nameModel = null;


	

	
	protected void addSaveButtons() {
		form.add(new SaveButton("savetop"));
		form.add(new SaveButton("savebottom"));
		
		form.add(new CancelButton("canceltop"));
		form.add(new CancelButton("cancelbottom"));
		
		
	}
	
	public class CancelButton extends IndicatingAjaxButton {
		private static final long serialVersionUID = 1L;

		private CancelButton(String id) {
			super(id);
		}

		@Override
		protected void onSubmit(AjaxRequestTarget target, Form form) {
			
			
			
		}
	}
	
	
	public class SaveButton extends IndicatingAjaxButton {
		private static final long serialVersionUID = 1L;

		private SaveButton(String id) {
			super(id);
		}

		@Override
		protected void onSubmit(AjaxRequestTarget target, Form form) {
			
			doValidatedSave(target);
			
		}
	}

	
	public DetailPanel (String id) {
		super(id);
		this.setOutputMarkupId(true);
	}


}
